package com.dd.front.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import com.dd.common.config.GlobalConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * web mvc 配置
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

//    @Value("${app.upload.dir:/opt/uploads}")
//    private String uploadDir;

    /**
     * 静态资源映射，将请求 /api/fetch/file/** 映射到项目根目录的/files/
     *
     * @param registry 资源处理器注册器
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 注意：末尾一点要带斜杠，表示该文件夹内
//        String saveDir = System.getProperty("user.dir") + File.separator + "files" + File.separator;
        registry.addResourceHandler("/" + GlobalConfig.publicPrefix + "/**")
                .addResourceLocations("file:" + GlobalConfig.fileSavePath);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        // 注册 Sa-Token 拦截器，打开注解式鉴权功能。
//        registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**");
        // 注册 Sa-Token 拦截器，打开注解式鉴权功能，并设置路由校验，规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
                .addPathPatterns("/**")
                .excludePathPatterns("/auth/login", "/auth/logout", "/auth/verify", "/auth/captcha", "/error")
                .excludePathPatterns("/" + GlobalConfig.publicPrefix + "/**")
                .excludePathPatterns(OpenApi3Config.SWAGGER_WHITELIST);
    }
}
